package com.candy.candymessage.business.controller;

import com.candy.candymessage.business.entity.MailRequest;
import com.candy.candymessage.business.entity.MailTemplate;
import com.candy.candymessage.business.service.IMailTemplateService;
import com.candy.candymessage.business.service.SendMailService;
import com.candy.candymessage.config.MailSenderConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author suYanLong
 * @date 2022年10月26日 16:13
 */
@EnableScheduling
@Slf4j
@RestController
public class SendMailController {

    @Autowired
    private SendMailService sendMailService;

    @Autowired
    IMailTemplateService mailTemplateService;

    @Autowired
    MailSenderConfig mailSenderConfig;

    List<MailTemplate> list = new ArrayList<>();

    @PostMapping("/simple")
    public void SendSimpleMessage(@RequestBody MailRequest mailRequest) {
        sendMailService.sendSimpleMail(mailRequest);
    }

    @PostMapping("/html")
    public void SendHtmlMessage(@RequestBody MailRequest mailRequest) {
        sendMailService.sendHtmlMail(mailRequest);
    }

//    @Scheduled(cron = "0/5 * * * * ?")
    public void sendMail() {
        MailRequest mailRequest = new MailRequest();
        MailTemplate mailTemplate = getMailTemplate();
        if (mailTemplate == null) {

            mailRequest.setSendTo("quan3713@tutanota.com");
            mailRequest.setSubject("向你支付比特币");
            mailRequest.setText("你好:\n" +
                    " 你不是很牛逼吗，盗我数据库呀！你继续盗呀，我给你支付比特币，这里问你下怎么获取比特币呀？xiaobizaizi");
        } else {
            mailRequest.setSendTo(mailTemplate.getSendTo());
            mailRequest.setSubject(mailTemplate.getSubject());
            mailRequest.setText(mailTemplate.getText());
        }

        try {
            sendMailService.sendSimpleMail(mailRequest);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Scheduled(cron = "0 0/10 * * * ?")
    public void refresh() {
        try {
            list = mailTemplateService.list();
            log.info(" refresh mailTemplate size: {}", list.size());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Scheduled(cron = "0 0/10 * * * ?")
    public void refreshMailConfig() {
        try {
            mailSenderConfig.buildMailSender();
            log.info(" refresh buildMailSender");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    int index = 0;

    public MailTemplate getMailTemplate() {
        if (list.isEmpty()) {
            return null;
        }
        return list.get(index++ % list.size());
    }

}
